#include <iostream>
#include <list>
#include <string>
#include <set>
using namespace std;

struct User
{
  string username;
  string ip;
};


int main()
{
  list<User> users;
  User tmp;
  bool existence;
  list<User>::iterator it;
  set<string> result;
  
  int casenumber;
  while (cin >> casenumber && casenumber)
  {
    users.clear();
    result.clear();
    
    while(casenumber--)
    {
      cin >> tmp.username >> tmp.ip;
      
      existence = false;
      for(it=users.begin();it!=users.end();++it)
      {
        if(it->ip == tmp.ip)
        {
          existence = true;
          break;
        }
      }
      
      if(existence)
      {
        result.insert(it->username + "|" + tmp.username + " is the MaJia of " + it->username);
      }
      else
      {
        users.push_back(tmp);
      }
    }
    
    for(set<string>::iterator result_it=result.begin();result_it!=result.end();++result_it)
    {
      cout << (*result_it).substr((*result_it).find("|")+1) << endl;
    }
    cout << endl;
  }
  
  return 0;
}

